package com.chess.annotation.demo;

import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.ViewConfiguration;
import android.widget.RelativeLayout;

public class ZoomRelativeLayoutV0 extends RelativeLayout implements ZoomViewDelegate {
	private float minScale = 1.0f;
	private float maxScale = 4.0f;
	private boolean enableMiniScale = true;
	public float initScale = 1.0f;
	private int mTouchSlop;
	private ZoomViewListener onTouchListener;
	private postZoomedImageActionDelegate delegate = null;

	public ZoomRelativeLayoutV0(Context context, AttributeSet attrs, int defaultStyle) {
		super(context, attrs, defaultStyle);
		init();
	}

	public ZoomRelativeLayoutV0(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ZoomRelativeLayoutV0(Context context) {
		super(context);
		init();
	}

	public ZoomRelativeLayoutV0(Context context, boolean miniScaleEnabled) {
		super(context);
		this.enableMiniScale = miniScaleEnabled;
		init();
	}

	private void init() {
		super.setClickable(true);
		final ViewConfiguration configuration = ViewConfiguration.get(getContext());
		mTouchSlop = configuration.getScaledTouchSlop();

		onTouchListener = new ZoomViewListener();
		onTouchListener.setDelegate(this);
		setOnTouchListener(onTouchListener);
	}

	private void postZoomedImageAction() {
		if (delegate != null) {
			delegate.postZoomedImageAction();
		}
	}

	private void preZoomedImageAction() {
		if (delegate != null) {
			delegate.preZoomedImageAction();
		}
	}

	public interface postZoomedImageActionDelegate {
		public void postZoomedImageAction();

		public void preZoomedImageAction();
	}

	public void setOnTouchListenerEnabled(boolean isEnabled) {
		if (isEnabled) {
			setOnTouchListener(onTouchListener);
		} else {
			setOnTouchListener(null);
		}
	}

	/**
	 * @param isEnabled
	 */
	public void setUsingMinimumScale(boolean isEnabled) {
		enableMiniScale = isEnabled;
	}

	public void setDelegate(postZoomedImageActionDelegate delegate) {
		this.delegate = delegate;
	}

	@Override
	public void initZoomViewAction(Matrix matrix) {

	}

	@Override
	public void preZoomViewAction(Matrix matrix) {
		preZoomedImageAction();
	}

	@Override
	public void postZoomViewAction(Matrix matrix) {
		postZoomedImageAction();
	}

	@Override
	public void adjustZoomViewScale(Matrix matrix, float midx, float midy) {
		float[] values = new float[9];
		matrix.getValues(values);

		float unadjustedScale = values[Matrix.MSCALE_X];

		float scale = 1;
		if (unadjustedScale >= initScale * maxScale) {
			scale = initScale * maxScale / unadjustedScale;
		} else if (enableMiniScale && unadjustedScale <= initScale * minScale) {
			scale = initScale * minScale / unadjustedScale;
		}
		matrix.postScale(scale, scale, midx, midy);
	}

	@Override
	public void adjustZoomViewPostion(Matrix matrix) {

	}

	@Override
	public void doZoomViewAction(Matrix matrix) {
		float[] values = new float[9];
		matrix.getValues(values);
		this.setScaleX(values[Matrix.MSCALE_X]);
		this.setScaleY(values[Matrix.MSCALE_Y]);
		this.setTranslationX(values[Matrix.MTRANS_X]);
		this.setTranslationY(values[Matrix.MTRANS_Y]);

	}

	@Override
	public float getTouchSlop() {
		return mTouchSlop;
	}

}